package com.dragon.modules.yuegou.service.impl.coupon;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.coupon.CouponTemplateRandomEntity;
import com.dragon.modules.yuegou.enums.code.CouponCode;
import com.dragon.modules.yuegou.mapper.coupon.CouponTemplateRandomMapper;
import com.dragon.modules.yuegou.model.coupon.CouponTemplateRandom;
import com.dragon.modules.yuegou.query.coupon.CouponTemplateRandomQuery;
import com.dragon.modules.yuegou.service.coupon.CouponTemplateRandomService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 随机券信息 服务实现
 *
 * @author pbuilder generated
 * @date 2020-06-29 19:18:21
 * @version 2.0
 */
@Service("coupCouponTemplateRandomService")
public class CouponTemplateRandomServiceImpl
		extends ServiceImpl<CouponTemplateRandomMapper, CouponTemplateRandomEntity>
		implements CouponTemplateRandomService {
	private Random random = new Random();

	@Override
	@Transactional
	public CouponTemplateRandom getTemplateRandom(String couponTemplateId) {
		//查询出所有随机段
		CouponTemplateRandomQuery query = new CouponTemplateRandomQuery();
		query.setCouponTemplateId(couponTemplateId);
		query.setValidStatus(true);
		List<CouponTemplateRandomEntity> list = list(query.toQueryWrapper());
		if (CollectionUtils.isEmpty(list)) {
			throw new DragonException(CouponCode.TEMPLATE_NOT_RANDOM);
		}
		// 可用的随机段
		CouponTemplateRandom couponTemplateRandom = getRandom(list);
		//依据随机段取随机数
		BigDecimal randomVal = getRandomReduction(couponTemplateRandom);
		if (couponTemplateRandom.getNowFee().add(randomVal).compareTo(couponTemplateRandom.getLimitMaxFee()) == 1) {
			BigDecimal diff = couponTemplateRandom.getLimitMaxFee().subtract(couponTemplateRandom.getNowFee());
			if (diff.compareTo(couponTemplateRandom.getBeginFee()) < 0) {
				throw new DragonException(CouponCode.TEMPLATE_RANDOM_ZERO);
			}else{
				randomVal = diff;
			}
		}
		int rs = baseMapper.updateReceive(couponTemplateRandom.getId(), randomVal, 1);
		if (rs < 1) {
			throw new DragonException(CouponCode.TEMPLATE_RANDOM_ZERO);
		}
		couponTemplateRandom.setReductionMount(randomVal);
		return couponTemplateRandom;
	}


	/**
	 * 在两个值之间取随机数 
	 */
	private BigDecimal getRandomReduction(CouponTemplateRandom couponTemplateRandom) {
		int start = couponTemplateRandom.getBeginFee().multiply(new BigDecimal(100)).intValue();
		BigDecimal endFee = couponTemplateRandom.getEndFee();
		BigDecimal limitMaxFee = couponTemplateRandom.getLimitMaxFee();
		BigDecimal nowFee = couponTemplateRandom.getNowFee();
		BigDecimal subtract = limitMaxFee.subtract(nowFee);
		BigDecimal fee =subtract.compareTo(endFee) < 0 ? subtract : endFee;
		if (subtract.compareTo(couponTemplateRandom.getBeginFee()) < 0) {
			fee = endFee;
		}
		int end = fee.multiply(new BigDecimal(100)).intValue();
		int val = random.nextInt(end - start + 1) + start;
		return new BigDecimal(val).divide(new BigDecimal(100), 2,BigDecimal.ROUND_HALF_EVEN);
	}
	
	/***
	 * 在随机段中取随机	
	 */
	private CouponTemplateRandom getRandom(List<CouponTemplateRandomEntity> list) {
		List<CouponTemplateRandom> randomList = new ArrayList<>();
		for (CouponTemplateRandomEntity entity : list) {
			Integer nowNum = entity.getNowNum();
			Integer limitMaxNum = entity.getLimitMaxNum();
			BigDecimal limitMaxFee = entity.getLimitMaxFee();
			BigDecimal nowFee = entity.getNowFee();
			BigDecimal beginFee = entity.getBeginFee();
			BigDecimal subtract = limitMaxFee.subtract(nowFee);
			if (nowNum < limitMaxNum && subtract.compareTo(beginFee) >= 0) {
				for (int i = 0; i < entity.getLimitMaxNum(); i++) {
					randomList.add(new CouponTemplateRandom(entity));
				}
			} else {
				entity.setValidStatus(false);
				entity.setModifiedTime(LocalDateTime.now());
				baseMapper.updateById(entity);
			}
		}
		//所有阶梯达到最大值时进行提示
		if (CollectionUtils.isEmpty(randomList)) {
			throw new DragonException(CouponCode.TEMPLATE_RANDOM_ZERO);
		}
		return randomList.get(random.nextInt(randomList.size()));
	}

	@Override
	public List<CouponTemplateRandom> randonList(String couponTemplateId) {
		CouponTemplateRandomQuery query = new CouponTemplateRandomQuery();
		query.setCouponTemplateId(couponTemplateId);
		List<CouponTemplateRandomEntity> entities = list(query.toQueryWrapper());
		return entities.stream().map(CouponTemplateRandom::new).collect(Collectors.toList());
	}

}